package na;


import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;



@Path("/student_info")
public class NAREST {
	
	@GET
	@Path("/search")
	@Produces("text/plain; charset=UTF-8")
	public String getName(@QueryParam("id")String id) {
		EntityManagerFactory factory =             
	            Persistence.createEntityManagerFactory("RESTNA");
		EntityManager manager = factory.createEntityManager();
	    EntityTransaction transaction = manager.getTransaction();
	    transaction.begin();
	    List results = null;
		try 
		{         
			String jpql = "select s.name from Student s where s.code = ?1";
			Query q = manager.createQuery(jpql);
			q.setParameter(1, id);
			results = q.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    transaction.commit();

	    
		if (results != null && results.size() > 0) {
			return results.get(0).toString();
		} else {
			return "";
		}
	}

}
